package org.example.ioc_demo; //启动类默认扫描这个目录下的注解以
                              // 及子目录的注解（spring的注解），然后加载相应程序
import org.example.ioc_demo.Component.UserComponent;
import org.example.ioc_demo.Controller.UserController;
import org.example.ioc_demo.Service.UserService;
import org.example.ioc_demo.config.BeanConfig;
import org.example.ioc_demo.config.UserConfig;
import org.example.ioc_demo.config.UserInfo;
import org.example.ioc_demo.repo.UserRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class IocDemoApplication {

    public static void main(String[] args) {
        //spring上下文
        ApplicationContext context =SpringApplication.run(IocDemoApplication.class, args);
        //从context中获取bean
        UserController bean = context.getBean(UserController.class);
        bean.doController();
        UserService bean2 = context.getBean(UserService.class);
        bean2.doService();

        //根据名称来获取bean
        UserService bean3 = (UserService) context.getBean("userService");
        bean3.doService();

        //根据名称和类型来获取bean
        UserController bean4 = context.getBean("userController", UserController.class);
        bean4.doController();

        //特殊情况：如果类名前两位都为大写，bean的名称为类名

        UserRepository bean5 = context.getBean("userRepository", UserRepository.class);
        bean5.doRepository();

        UserComponent bean6 = context.getBean("userComponent", UserComponent.class);
        bean6.doComponent();

        UserConfig bean7 = context.getBean("userConfig", UserConfig.class);
        bean7.doConfig();
        System.out.println("第一次取的UserConfig对象的引用:"+bean7);

        UserConfig bean8 = context.getBean("userConfig", UserConfig.class);
        System.out.println("第二次取的UserConfig对象的引用:"+bean8);
        //说明通过五大类注解取到的对象都是同一个对象

        //@Bean 演示
        UserInfo userInfo = (UserInfo)context.getBean("userInfo");
        System.out.println(userInfo);

        UserInfo userInfo2 = context.getBean("userInfo2", UserInfo.class);
        System.out.println(userInfo2);


    }

}
